/* ====================================================================
 * (#)Header: EmaInd.java
 * (#)Revision: 
 * (#)Date Created: Apr 9, 2013
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2013 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.stock.indicator;

import com.karolak.math.formula.Ema;
import com.karolak.math.formula.K;
import com.karolak.stock.data.DataManager;

/**
 * <code>EmaInd</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class EmaInd extends Indicator {
	
	private double value;
	private double k;

	public EmaInd(DataManager dataManager, int period, Indicator input) {
		super(dataManager, period, input);
		setName(String.format("Ema%s%s", getInput().getName(), getPeriod()));
		setK(K.calculate(getPeriod()));
	}
	
	public EmaInd(String name, DataManager dataManager, int period, Indicator input) {
		super(name, dataManager, period, input);
		setK(K.calculate(getPeriod()));
	}

	@Override
	public double execute(double x) {
		if (getInput() != null) {
			x = getDataManager().getLast(getInput().getName());
		}
		if (getValue() == 0) {
			setValue(x);
		}
		double x0 = Ema.calculate(getPeriod(), getValue(), x);
		setValue(x0);
		return getValue();
	}

	public double getValue() {
		return value;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public double getK() {
		return k;
	}

	public void setK(double k) {
		this.k = k;
	}
}
